/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：WelcomeDaoImpl.java
 * History: Nov 13, 2013: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.math.BigInteger;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IWelcomeDao;
import com.xteam.ecsystem.entities.BaseGoodsInfo;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.entities.BaseWebCounter;

/**
 * 欢迎界面数据初始化实现类
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class WelcomeDaoImpl implements IWelcomeDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public Long loadWqrdd() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseOrderInfo.REF + " where boiOrder = 0";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadDzfdd() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseOrderInfo.REF + " where boiOrder = 1 and boiPay = 0";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}

	public Long loadDfhdd() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseOrderInfo.REF + " where boiShip = 2";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadYcjdd() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseOrderInfo.REF + " where boiShip = 4";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadSpzsm() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF;
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadSpjgs() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF + " where bgiNumber <= bgiWarn";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadJptjs() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF + " where bgiBest = 1";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadXptjs() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF + " where bgiNew = 1";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}

	public Long loadRxsps() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF + " where bgiHot = 1";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}

	public Long loadTgsps() {
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseGoodsInfo.REF + " where bgiBuy = 1";
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}

	public void savePageViews(BaseWebCounter webCounter){
		try {
			this.baseDao.save(webCounter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Long loadWebfwzs(){
		Long count = 0l;
		
		try {
			String hql = "select count(*) from " + BaseWebCounter.REF;
			count = this.baseDao.count(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadWebjrfw(){
		Long count = 0l;
		Calendar c = Calendar.getInstance();
		
		try {
			String hql = "select count(*) from " + BaseWebCounter.REF + " where bwcYear=? and "
					   + "bwcMonth=? and bwcDate=?";
			
			count = this.baseDao.count(hql, new Object[]{
					c.get(Calendar.YEAR), 
					c.get(Calendar.MONTH) + 1,
					c.get(Calendar.DATE)
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadWebmrpj(){
		Long count = 0l;
		String hql = "";
		
		try {
			hql = "from " + BaseWebCounter.REF + " order by bwcYear desc, bwcMonth desc, bwcDate desc, " 
				+ "bwcHour desc, bwcMinute desc limit 0,1";
			BaseWebCounter maxDateBwc = (BaseWebCounter)this.baseDao.findObject(hql, null);
			
			if (maxDateBwc != null) {
				hql = "from " + BaseWebCounter.REF + " order by bwcYear asc, bwcMonth asc, bwcDate asc, " 
					+ "bwcHour asc, bwcMinute asc limit 0,1";
				BaseWebCounter minDateBwc = (BaseWebCounter)this.baseDao.findObject(hql, null);
				
				Calendar maxc = Calendar.getInstance();  
				maxc.set(maxDateBwc.getBwcYear(), maxDateBwc.getBwcMonth() - 1, maxDateBwc.getBwcDate(), 
						maxDateBwc.getBwcHour(), maxDateBwc.getBwcMinute());
				
				Calendar minc = Calendar.getInstance(); 
				minc.set(minDateBwc.getBwcYear(), minDateBwc.getBwcMonth() - 1, minDateBwc.getBwcDate(),
						minDateBwc.getBwcHour(), minDateBwc.getBwcMinute());
				
				long maxTimeMs = maxc.getTimeInMillis(); 
				long minTimeMs = minc.getTimeInMillis();  
				long betweenDays = (maxTimeMs - minTimeMs) / (1000 * 3600 * 24) + 1; 
				count = (loadWebfwzs() / betweenDays);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public Long loadWeblszg(){
		Long count = 0l;
		
		try {
			String sql = "select max(mc) as maxcount from (select count(*) as mc from " 
					   + "base_web_counter group by bwc_year,bwc_month,bwc_date) as temp";
			
			BigInteger bi = this.baseDao.countWithAggFunSQL(sql, null);
			if (bi != null) {
				count = bi.longValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
}
